#include "tcp.h"

int main(int argc, char*argv[]){

	int fd, newfd;
	int ret;
	char buf[BUFSIZ] = {};
	Addr_in addr, client_addr;
	socklen_t addrlen = sizeof(addr);

	/*检查参数*/
	Argment(argc, argv);

	/*创建套接字*/
	fd = SocketInit(argv, true);	

	/*接受客户端连接*/
	do{
		newfd = accept(fd, (Addr *)&client_addr, &addrlen);
	}while(newfd < 0 && errno == EINTR); //如果信号导致中断 继续执行
	if(newfd < 0)
		ErrExit("accept");

	/*接受客户端数据*/
	while(1){
		do {
			ret = recv(newfd, buf, BUFSIZ, 0);
		}while(ret < 0 && errno == EINTR);
		if(ret < 0)
			ErrExit("recv");
		else if(!ret)
			break;
		else
			printf("[%s:%d]buf:%s\n", 
					inet_ntoa(client_addr.sin_addr), 
					ntohs(client_addr.sin_port), buf);
	}

	close(newfd);
	close(fd);	
	return 0;
}
